#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <math.h>
#include "include/CRsa32.h"
#include <string.h>
#include <cryptopp/randpool.h>   
using namespace std;
using namespace CryptoPP;  

void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);   
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);   
string RSADecryptString(const char *privFilename, const char *ciphertext);   
RandomPool & GlobalRNG();   
/*
Euclid?㷨???˷???ԪEuclid?㷨???˷???Ԫ???檥
ab ??1 ( mod r)      a,b????r ??Ϊ????
ŷ???????㷨??ab ??1( mod r)  ??֪??a, ??b
r = aq1 + c1                     ?C?C (1)
a =c1q2+c2                      ?C?C (2)
c1=c2q3+c3                     ?C?C (3)
c2=c3q3+c4
?? ??
c(j-2)=c(j-1)q(j)+c(j)       ?C?C (j)
c(j-1)=c(j)q(j+1)+1          ?C?C (j+1)
???ƹ?ʽ??
b(-1)=0
b(0)=1
b(j)=(?C1)??b(j?C1)??q(j)+b(j?C2)
*/
/*
s = (p-1)*(q-1) = 2668
2668 - 42 * 63 = 22 = 2668 mod 63
63 - 2 * 22 = 19 = 63 mod 22
22 - 19 = 3 = 22 mod 19
19 - 6 * 3 = 1 = 19 mod 3

???ˣ?

1 = 19 - 6 * 3 = 19 - 6 * (22-19) = 7*19 - 6*22 = 7*(63-2*22)-6*22= 7*63 - 20*22 = 7*63 - 20*(2668-42*63) = 847*63 - 20*2668

1 = 847*63 - 20*2668

????d=847
*/
void calcEuclid(int a, int r)
{
    int tempr;
    //c1 = c2;
    //c2 = tempr;
    while(a>0)
    {
        cout<<"a = "<<a<<"  "<<"r = "<<r<<"  "<<"tempr = "<<tempr<<endl;
        tempr = r%a;
        r = a;
        a = tempr;
    }
    cout<<"a = "<<a<<"  "<<"tempr = "<<tempr<<endl;
}

int niyuan(int n,int u)//????Ԫ
{
    int n1=n;
    int n2=u;
    int b1=0;
    int b2=1;
    int q=n1/n2;
    int r=n1-q*n2;
    while(r!=0)
    {
        n1=n2;
        n2=r;
        int t=b2;
        b2=b1-q*b2;
        b1=t;
        q=n1/n2;
        r=n1-q*n2;
    }
    if(n2!=1)
    {
        return 0 ;
    }
    else
    {
        return (b2+n)%n;
    }
}

/*????????Լ??*/
int Euclid_Algorithm (int m, int n)
{
        int temp = m;
        if (!m || !n) return 0;
        if (m < n)    {m = n; n = temp;}
        while (1) {
                if (!(m = m % n)) return n;
                if (!(n = n % m)) return m;
        }
}

int main()
{
    //calcEuclid(550,1769);
    //cout<<niyuan(1769,550)<<endl;
    //cout<<Euclid_Algorithm(550,1769)<<endl;
    //srand(unsigned(time(0)));
    //cout <<"RAND_MAX = "<<RAND_MAX << endl;
    //cout<<"0xffffffff/RAND_MAX = "<<0xffffffff/RAND_MAX<<endl;
    //cout<<"0xffffffff%RAND_MAX = "<<0xffffffff%RAND_MAX<<endl;
    //cout<<"0xffffffff/RAND_MAX/RAND_MAX = "<<0xffffffff/RAND_MAX/RAND_MAX<<endl;
    //cout<<"(0xffffffff/RAND_MAX)%RAND_MAX = "<<(0xffffffff/RAND_MAX)%RAND_MAX<<endl;
    //cout<<sizeof(long long)<<endl;
    //cout<<sizeof(ULONG)<<endl;
    //cout<<hex<<(((rand()<<1) + rand()%2)<<16)<<endl;
    //cout<<hex<<(0x1122334455667788&0xFF00000000000000)<<endl;
    //cout<<10%0<<endl;
    //CRsa32 rsa;
    //rsa.Calc();
        //cout<<(unsigned int)(rand()/(RAND_MAX + 1.0)*0xffffffff)<<endl;
    char priKey[128] = {0};   
    char pubKey[128] = {0};   
    char seed[1024] = {0};   
  
    // 生成 RSA 密钥对  
    strcpy(priKey, "pri"); // 生成的私钥文件  
    strcpy(pubKey, "pub"); // 生成的公钥文件  
    strcpy(seed, "seed");   
    GenerateRSAKey(1024, priKey, pubKey, seed);   
    cout<<"prikey = "<<priKey<<endl;
    cout<<"pubkey = "<<pubKey<<endl;
    cout<<"seed = "<<seed<<endl;

    //RSA 加解密  
    char message[1024] = {0};   
    cout<<"Origin Text:\t"<<"just a test!"<<endl<<endl;   
    strcpy(message, "just a test!");   
    string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密  
    cout<<"Encrypted Text:\t"<<encryptedText<<endl<<endl;   
    string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA  解密  
    cout<<"Decrypted Text:\t"<<decryptedText<<endl<<endl;  
    return 0;
}
